﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Storage = JeanFish.Blog.Storage;
using Business = JeanFish.Blog.Business;
using System.Threading;
using System.IO;
using System.Threading.Tasks;

namespace JeanFish.Blog
{
    public sealed class BlogService
    {
        static BlogService instance = null;
        public static BlogService Current
        {
            get
            {
                return instance;
            }
        }
        public static void SetBlogProvider(Storage.IBlogDataProvider blogDataProvider,Storage.IBlogMediaProvider blogMediaProvider)
        {
            instance = new BlogService(blogDataProvider,blogMediaProvider);
        }

        public Business.Blog Blog { get; private set; }
        private Storage.IBlogDataProvider DataProvider { get; set; }
        private Storage.IBlogMediaProvider MediaProvider { get; set; }
        private BlogService(Storage.IBlogDataProvider blogDataProvider,Storage.IBlogMediaProvider blogMediaProvider)
        {
            this.DataProvider = blogDataProvider;
            this.MediaProvider = blogMediaProvider;
        }
        public async Task LoadBlogAsync(object state=null)
        {
            this.Blog= await DataProvider.LoadBlogAsync();
            await this.Blog.LoadAsync();
        }
        public async Task<IEnumerable<Business.Post>> ListPostsAsync(Business.Blog blog, Paging paging)
        {
            var postList = await DataProvider.ListPostsAsync(blog, paging);
            if (postList == null) return new List<Business.Post>();
            return postList.Select(t =>
            {
                Business.Post p = t;
                return p;
            }).ToList();
        }
        public async Task<Business.User> GetPostAuthorAsync(Business.Post post)
        {
            return await DataProvider.GetPostAuthorAsync(post);
        }
        public async Task<Business.User> LoadBlogOnwerAsync(Business.Blog blog)
        {
            return await DataProvider.LoadBlogOnwerAsync(blog);
        }
        public async Task<SavedResult> SaveBlogAsync(Business.Blog blog)
        {
            return await DataProvider.SaveBlogAsync(blog);
        }
        public async Task<SavedResult> CreatePostAsync(Business.Post post)
        {
            if (!post.IsNew) return SavedResult.Oops;
            var result = await DataProvider.CreatePostAsync(post);
            if (result == SavedResult.OK)
            {
                post.IsNew = false;
            }
            return result;
        }
        public async Task<SavedResult> UpdatePostAsync(Business.Post post)
        {
            if (post.IsNew) return SavedResult.Oops;
            return await DataProvider.UpdatePostAsync(post);
        }
        public async Task<Business.Post> GetPostAsync(Business.Blog blog, Guid postId)
        {
            var pid = postId.ToString("N");
            return await DataProvider.GetPostAsync(blog, pid);
        }
        public async Task<Uri> SaveMediaAsync(Business.Blog blog,string mediaName,Stream mediaData)
        {
            return await MediaProvider.SaveMediaAsync(blog,mediaName, mediaData);
        }
    }
}